package com.bookRecommend.controller;

import com.bookRecommend.commons.ResponseUtils;
import com.bookRecommend.model.Admin;
import com.bookRecommend.service.IAdminService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin")

public class AdminController {
    @Resource
    IAdminService adminService;

    @RequestMapping
    public ResponseUtils findAll() {
        List<Admin> allAdmin = adminService.findAllAdmin();
        if (allAdmin != null) {
            return ResponseUtils.success("", allAdmin);
        }
        return ResponseUtils.failure("查找失败！");
    }

    @GetMapping("/login/{username}/{password}/{captcha}")
    public ResponseUtils login(@PathVariable String username, @PathVariable String password, @PathVariable String captcha, HttpServletRequest request) {
//        if (!CaptchaUtil.ver(captcha, request)) {
//            return ResponseUtils.failure("验证码错误！");
//        }
        Admin admin = adminService.findByUserNameANdPassword(username, password);
        if (admin != null) {
            return ResponseUtils.success("登录成功", admin);
        } else {
            return ResponseUtils.failure("用户名或密码错误!");
        }
    }

    @RequestMapping("/register")
    public ResponseUtils register(String username, String password, String captcha, HttpServletRequest request) {
        if (!CaptchaUtil.ver(captcha, request)) {
            return ResponseUtils.failure("验证码错误！");
        }
        int flag = adminService.addAdmin(username, password);
        if (flag > 0) {
            return ResponseUtils.success("注册成功");
        } else {
            return ResponseUtils.failure("用户名或密码错误!");
        }
    }

    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        try {
            CaptchaUtil.out(130, 50, 4, request, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/update")
    public ResponseUtils modify(String password, Integer id) {
        int flag = adminService.modify(password, id);
        if (flag > 0) {
            return ResponseUtils.success("修改成功！");
        }
        return ResponseUtils.failure("修改失败！");
    }

    /**
     * 修改密码查询密码是否输入正确
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping("/{username}/{password}")
    public ResponseUtils find(@PathVariable String username, @PathVariable String password) {
        Admin admin = adminService.findByUserNameANdPassword(username, password);
        if (admin != null) {
            return ResponseUtils.success("正确！");
        }
        return ResponseUtils.failure("错误！");
    }
}
